Imports System.IO

Public Class RuntimeReport

    Public Event Scanning(ByVal path As String)
    Public Event RuntimeFound(ByVal path As String)

    Private _startLocation As String

    Private Const RuntimeDirectory As String = "runtime"

    Private Const LibrariesDirectory As String = "libraries"
    Private Const GeometriesDirectory As String = "geometries"
    Private Const TexturesDirectory As String = "textures"

    Private Const CharacterDirectory As String = "character"
    Private Const ExpressionDirectory As String = "face"
    Private Const HairDirectory As String = "hair"
    Private Const LightDirectory As String = "light"
    Private Const PoseDirectory As String = "pose"
    Private Const PropsDirectory As String = "props"

    Private Const PathSeparator As String = "\"

    'Public Sub New(ByVal startLocation As String)
    '    _startLocation = startLocation
    'End Sub

    Public Sub New()

    End Sub

    Private Sub CheckReportsDirectory()
        If Not Directory.Exists(My.Settings.ReportsDirectory) Then
            Directory.CreateDirectory(My.Settings.ReportsDirectory)
        End If
    End Sub

    Private Shared Function IsRuntime(ByVal directoryToScan As DirectoryInfo) As Boolean

        Dim result As Boolean = False
        Dim librariesPath As String = directoryToScan.FullName & PathSeparator & LibrariesDirectory
        If directoryToScan.Name.ToLower = RuntimeDirectory Then
            If Directory.Exists(librariesPath) Then
                result = True
            End If
        End If

        Return result

    End Function

    Private Sub ScanForRuntimes(ByVal directoryToScan As DirectoryInfo, _
                                       ByVal foundRuntimes As System.Collections.ObjectModel.Collection(Of DirectoryInfo))

        If (directoryToScan.Name.ToLower <> LibrariesDirectory And _
           directoryToScan.Name.ToLower <> GeometriesDirectory And _
           directoryToScan.Name.ToLower <> TexturesDirectory) And _
           directoryToScan.Parent.Name.ToLower <> RuntimeDirectory Then

            RaiseEvent Scanning(directoryToScan.FullName)
            If IsRuntime(directoryToScan) Then
                RaiseEvent RuntimeFound(directoryToScan.Parent.FullName)
                foundRuntimes.Add(directoryToScan)
            End If

            For Each subDirectory As DirectoryInfo In directoryToScan.GetDirectories
                ScanForRuntimes(subDirectory, _
                                foundRuntimes)
            Next
        End If

    End Sub

    Public Function ScanForRuntimes(ByVal directoryToScan As String) As System.Collections.ObjectModel.Collection(Of DirectoryInfo)

        Dim foundRuntimes As New System.Collections.ObjectModel.Collection(Of DirectoryInfo)
        If Directory.Exists(directoryToScan) Then

            Dim scanDirectory As DirectoryInfo = New DirectoryInfo(directoryToScan)

            RaiseEvent Scanning(scanDirectory.FullName)
            If IsRuntime(scanDirectory) Then
                foundRuntimes.Add(scanDirectory)
                RaiseEvent RuntimeFound(scanDirectory.FullName)
            End If

            For Each subDirectory As DirectoryInfo In scanDirectory.GetDirectories
                ScanForRuntimes(subDirectory, _
                                foundRuntimes)
            Next

        End If

        Return foundRuntimes

    End Function

    Public Sub GenerateReport()

        Try

            CheckReportsDirectory()

        Catch ex As IOException
            MsgBox(ex.Message)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

End Class
